<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 服务管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>

                <el-select v-model="query.oneId" placeholder="分类" class="handle-select mr10" @change="chooseSecondType($event, 1)">
                    <el-option v-for="item in tableData" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                </el-select>

                <el-select v-model="query.twoId" placeholder="二级分类" class="handle-select mr10" @change="chooseSecondType($event, 2)">
                    <el-option v-for="item in secondTableData" :key="item.id" :label="item.secondMenu" :value="item.id"></el-option>
                </el-select>

                <el-button type="primary" icon="el-icon-search" class="handle-del mr10" @click="selectShopByType(1)">查询该分类</el-button>

                <el-button type="primary" icon="el-icon-search" class="handle-del mr10" @click="updateType">编辑选择的分类</el-button>

                <el-button type="primary" icon="el-icon-circle-plus-outline" class="handle-del mr10" @click="addServiceShop"
                    >新增/删除分类</el-button
                >

                <el-input v-model="query.name" placeholder="服务名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="selectShopByType(2)">搜索</el-button>
            </div>
            <el-table
                :data="serviceShopData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="服务ID" width="55" align="center"></el-table-column>
                <el-table-column prop="typeName" label="一级分类"></el-table-column>
                <el-table-column prop="secondMenu" label="二级分类"></el-table-column>
                <el-table-column prop="serviceName" label="服务名称"></el-table-column>
                <el-table-column label="原价">
                    <template slot-scope="scope">￥{{ scope.row.servicePrice }}</template>
                </el-table-column>
                <el-table-column label="现价">
                    <template slot-scope="scope">￥{{ scope.row.serviceDiscountPrice }}</template>
                </el-table-column>
                <el-table-column prop="shopName" label="门店名称"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改信息</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改信息" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="选择一级分类">
                    <el-select v-model="form.oneId" placeholder="分类" class="handle-select mr10" @change="formChooseSecondType">
                        <el-option v-for="item in tableData" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="选择二级分类">
                    <el-select v-model="form.twoId" placeholder="分类" class="handle-select mr10">
                        <el-option v-for="item in formSecondTableData" :key="item.id" :label="item.secondMenu" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="服务名称">
                    <el-input v-model="form.serviceName" style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="原价">
                    <el-input v-model="form.servicePrice" style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="现价">
                    <el-input v-model="form.serviceDiscountPrice" style="width: 150px"></el-input>
                </el-form-item>
                <el-form-item label="所属门店">
                    <div v-text="form.shopName"></div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增 -->
        <el-dialog title="新增/删除分类" :visible.sync="editVisibleT" width="30%">
            <el-form ref="form" :model="addform" label-width="150px">
                <el-form-item label="查看已有分类："></el-form-item>

                <el-form-item label="选择一级分类">
                    <el-select
                        v-model="addform.oneId"
                        placeholder="分类"
                        class="handle-select mr10"
                        @change="formChooseSecondType($event, 2)"
                    >
                        <el-option v-for="item in tableData" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                    </el-select>
                    <el-button type="danger" @click="deleteServiceType(1)">删除当前选择</el-button>
                </el-form-item>

                <el-form-item label="选择二级分类">
                    <el-select v-model="addform.twoId" placeholder="分类" class="handle-select mr10">
                        <el-option v-for="item in formSecondTableData" :key="item.id" :label="item.secondMenu" :value="item.id"></el-option>
                    </el-select>
                    <el-button type="danger" @click="deleteServiceType(2)">删除当前选择</el-button>
                </el-form-item>

                <el-form-item label="新增分类："></el-form-item>

                <el-form-item label="新增一级分类">
                    <el-input v-model="addform.typeName" style="width: 150px"></el-input>
                    &#12288;
                    <el-button type="primary" @click="addServiceType(1)">新增</el-button>
                </el-form-item>

                <el-form-item label="新增二级分类">
                    <el-input v-model="addform.secondMenu" style="width: 150px" placeholder="先选择一级分类"></el-input>
                    &#12288;
                    <el-button type="primary" @click="addServiceType(2)">新增</el-button>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisibleT = false">取 消</el-button>
            </span>
        </el-dialog>

        <!-- 编辑分类 -->
        <el-dialog title="编辑分类" :visible.sync="editVisibleF" width="30%">
            <el-form ref="form" :model="updateform" label-width="150px">
                <el-form-item label="修改一级分类：">
                    <el-input v-model="updateform.typeName" placeholder="输入新的名字" style="width: 120px"></el-input>
                    &#12288;
                    <el-button type="primary" @click="updateServiceType(1)">修改</el-button>
                </el-form-item>
                <el-form-item label="修改二级分类：" v-show="updateform.twoId != 0">
                    <el-input v-model="updateform.secondMenu" placeholder="输入新的名字" style="width: 120px"></el-input>
                    &#12288;
                    <el-button type="primary" @click="updateServiceType(2)">修改</el-button>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisibleF = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
import qs from 'qs';
import {
    getServerType,
    getServerShop,
    updateServerShop,
    addServerType,
    deleteServerType,
    updateServerType,
    deleteServerShop
} from '../../api/server';
export default {
    name: 'basetable',
    data() {
        return {
            //所有服务数据
            serviceShopData: [],
            //所有分类数据
            tableData: [],
            //二级分类数据
            secondTableData: [],
            //表单独立的二级分类数据
            formSecondTableData: [],
            query: {
                typeName: '',
                secondType: '',
                oneId: '',
                twoId: '',
                name: '',
                type: '',
                pageIndex: 1,
                pageSize: 5
            },
            form: {},
            addform: {
                oneId: '',
                typeName: '',
                twoId: '',
                secondMenu: '',
                type: '',
                deleteType: ''
            },
            updateform: {
                oneId: '',
                typeName: '',
                twoId: '',
                secondMenu: '',
                type: ''
            },
            ids: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            editVisibleT: false,
            editVisibleF: false,
            pageTotal: 0,
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        //获取所有数据
        getData() {
            //重置数据
            this.secondTableData = '';
            this.formSecondTableData = '';
            this.query.oneId = '';
            this.query.twoId = '';
            this.getServiceType();
            this.getServiceShop();
        },

        // 获取一二级的分类数据
        getServiceType() {
            getServerType().then((res) => {
                this.tableData = JSON.parse(JSON.stringify(res.data));
            });
        },

        //分页获取所有服务
        getServiceShop(type) {
            this.query.type = type;
            console.log(this.query);
            getServerShop(this.query).then((res) => {
                console.log(res);
                this.serviceShopData = JSON.parse(JSON.stringify(res.data.list));
                this.pageTotal = res.data.total || 0;
            });
        },

        //发送修改详情请求
        updateServiceShop() {
            updateServerShop(this.form).then((res) => {
                if (res.code == 200) {
                    this.getData();
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                } else {
                    this.$message.error('修改失败');
                }
                // this.$set(this.tableData, this.idx, this.form);
            });
        },

        //发送修改分类请求
        updateServiceType(type) {
            this.updateform.type = type;
            updateServerType(this.updateform).then((res) => {
                if (res.code == 200) {
                    this.getData();
                    this.editVisibleF = false;
                    this.$message.success('修改成功');
                } else {
                    this.$message.error('修改失败');
                }
            });
        },

        //提交新增分类数据
        addServiceType(type) {
            this.addform.type = type;
            console.log(this.addform);
            addServerType(this.addform).then((res) => {
                if (res.code == 200) {
                    this.$message.success(res.msg);
                    this.getData();
                    this.addform.typeName = '';
                    this.addform.secondMenu = '';
                    this.addform.oneId = '';
                    this.addform.twoId = '';
                    this.formSecondTableData = '';
                } else {
                    this.$message.error(res.msg);
                }
            });
        },

        //删除分类
        deleteServiceType(type) {
            var re = confirm('确定要删除这个分类吗');
            if (!re) {
                return;
            }
            if (type == 1 && this.addform.oneId == 0) {
                this.$message.error('不能删除这个分类');
                return;
            } else if (type == 2 && this.addform.twoId == 0) {
                this.$message.error('不能删除这个分类');
                return;
            }
            this.addform.deleteType = type;
            deleteServerType(this.addform).then((res) => {
                if (res.code == 200) {
                    this.$message.success('删除成功');
                    this.getData();
                    this.addform.oneId = '';
                    this.addform.twoId = '';
                    this.formSecondTableData = '';
                } else {
                    this.$message.error(res.msg);
                }
            });
        },

        //触发选择二级菜单按钮
        chooseSecondType(id, type) {
            if (type == 1) {
                for (var i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].id == id) {
                        this.secondTableData = this.tableData[i].serviceTypeSeconds;
                        this.query.typeName = this.tableData[i].typeName;
                    }
                }
                //重置二级分类按钮
                this.query.twoId = '';
            } else if (type == 2) {
                for (var i = 0; i < this.secondTableData.length; i++) {
                    if (this.secondTableData[i].id == id) {
                        this.query.secondType = this.secondTableData[i].secondMenu;
                    }
                }
            }
        },

        //表单中触发修改二级菜单
        formChooseSecondType(id, num) {
            for (var i = 0; i < this.tableData.length; i++) {
                if (this.tableData[i].id == id) {
                    this.formSecondTableData = this.tableData[i].serviceTypeSeconds;
                }
            }
            //重置二级分类按钮
            if (!num) {
                this.form.twoId = '';
            } else if (num == 2) {
                this.addform.twoId = '';
            }
        },

        //修改分类信息
        updateType() {
            if (this.query.oneId == 0 || this.query.oneId == '') {
                this.$message.error('请先选择一个分类,且不能选择未分类编辑');
                return;
            }
            this.updateform.oneId = this.query.oneId;
            this.updateform.twoId = this.query.twoId;
            this.updateform.typeName = this.query.typeName;
            this.updateform.secondMenu = this.query.secondType;
            this.editVisibleF = true;
        },

        //触发分类查询按钮
        selectShopByType(type) {
            if (type == 1) {
                this.getServiceShop(type);
            } else if (type == 2) {
                this.getServiceShop(type);
            } else {
                this.$message.error('数据异常');
            }
        },

        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log(row);
                    deleteServerShop({ids:row.id}).then((res) => {
                        if (res.code == 200) {
                            this.$message.success('删除成功');
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //多选删除
        delAllSelection() {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        console.log(this.multipleSelection[i].id);
                        this.ids[i] = this.multipleSelection[i].id;
                    }
                    axios
                        .post('http://funvalue.top:23323/carBeautyChange/deleteServiceShop', qs.stringify({ ids: this.ids }, { indices: false }))
                        .then((res) => {
                            if (res.data.code == 200) {
                                this.getData();
                                const length = this.multipleSelection.length;
                                let str = '';
                                this.delList = this.delList.concat(this.multipleSelection);
                                for (let i = 0; i < length; i++) {
                                    str += this.multipleSelection[i].serviceName + ' ';
                                }
                                this.$message.success('删除成功');
                                this.multipleSelection = [];
                            } else {
                                this.$message.error(this.res.msg);
                            }
                        });
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = JSON.parse(JSON.stringify(index));
            this.form = JSON.parse(JSON.stringify(row));
            //在加载编辑框时对表单的二级菜单进行操作
            this.formChooseSecondType(this.form.oneId, 1);
            this.editVisible = true;
            console.log(row);
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.updateServiceShop(this.form);
        },
        //新增分类表单
        addServiceShop() {
            this.editVisibleT = true;
            //清空表单
            this.addform = this.$options.data().addform;
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getServiceShop();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
